import Vue from 'vue'
import Router from 'vue-router'
import { AppModule, DirectionType } from '@/store/modules/app.ts'

Vue.use(Router)

const router = new Router({
    mode: 'hash',
    scrollBehavior: (...arg) => arg[2] || { x: 0, y: 0 },
    base: process.env.NODE_ENV === 'production' ? '/qc-h5' : '/',
    routes: [
        {
            path: '/create',
            component: () => import('@/views/home'),
            meta: {
                name: '发起质控',
                hideHead: false,
                showBack: true
            }
        },
        {
            path: '/login',
            component: () => import('@/views/login'),
            meta: {
                name: '登录',
                hideHead: true,
                showBack: false
            }
        },
        {
            path: '/search',
            component: () => import('@/views/search'),
            meta: {
                name: '查询质控',
                hideHead: false,
                showBack: true,
                hideShadow: true
            }
        },
        {
            path: '/detail',
            component: () => import('@/views/search/detail.tsx'),
            meta: {
                name: '质控详情',
                hideHead: false,
                showBack: true
            }
        },
        {
            path: '/result',
            component: () => import('@/views/result'),
            meta: {
                name: '质控结果查询',
                hideHead: false,
                showBack: true
            }
        },
        {
            path: '/task-statistic',
            component: () => import('@/views/statistic/task.tsx'),
            meta: {
                name: '任务报表',
                hideHead: false,
                showBack: true
            }
        },
        {
            path: '/result-statistic',
            component: () => import('@/views/statistic/result.tsx'),
            meta: {
                name: '质控结果报表',
                hideHead: false,
                showBack: true
            }
        },
        // task-statistic
        {
            path: '/',
            component: () => import('@/views/index'),
            meta: {
                name: '质控平台',
                hideHead: false
            }
        },
        {
            path: '/done',
            component: () => import('@/views/done'),
            meta: {
                name: '提交成功',
                hideHead: false
            }
        },
        {
            path: '/404',
            component: () => import('@/views/excep'),
            meta: {
                name: '页面找不到了',
                hideHead: false
            }
        },
        {
            path: '*',
            redirect: '/404',
            meta: {
                hideHead: true
            }
        }
    ]
})

const history = window.sessionStorage
const count: any = history.getItem('count') || 1
let historyCount: any = count * 1

router.beforeEach((to, from, next) => {
    const toIndex = history.getItem(to.path)
    const fromIndex = history.getItem(from.path)
    const operatorId = localStorage.getItem('operatorId')
    if (!operatorId && to.path != '/login') {
        next('/login')
    }
    if (toIndex) {
        if (
            !fromIndex ||
            parseInt(toIndex, 10) > parseInt(fromIndex, 10) ||
            (toIndex === '0' && fromIndex === '0')
        ) {
            AppModule.ToggleDirection(DirectionType.forward)
        } else {
            AppModule.ToggleDirection(DirectionType.reverse)
        }
    } else {
        ++historyCount
        history.setItem('count', historyCount)
        to.path !== '/' && history.setItem(to.path, historyCount)
        AppModule.ToggleDirection(DirectionType.forward)
    }
    if (/\/http/.test(to.path)) {
        const url = to.path.split('http')[1]
        window.location.href = `http${url}`
    } else {
        next()
    }
})
router.afterEach(to => {
    AppModule.ToggleHead(to.meta.hideHead)
    AppModule.ToggleBack(to.meta.showBack)
    AppModule.ToggleTitle(to.meta.name)
    AppModule.ToggleShadow(to.meta.hideShadow)
})

export default router
